package com.Students.dao.impl;

import com.DBservice.DBservice;
import com.Students.dao.IStudentDao;
import com.Students.pojo.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentDao implements IStudentDao {
    private DBservice dBservice = DBservice.getinstance();

    @Override
    public int add(Student student) {
        String sql = "insert into students(stu_id,stu_name,stu_clazz,stu_phone) values(";
        sql = sql + "'" + student.getStu_ID() + "',";
        sql = sql + "'" + student.getStu_name() + "',";
        sql = sql + "'" + student.getStu_clazz() + "',";
        sql = sql + "'" + student.getStu_phone() + "')";

        return this.dBservice.updateSql(sql);
    }

    @Override
    public int update(Student student) {
        String sql = "update students set ";
        sql = sql + "stu_id='" + student.getStu_ID() + "',";
        sql = sql + "stu_name='" + student.getStu_name() + "',";
        sql = sql + "stu_clazz='" + student.getStu_clazz() + "',";
        sql = sql + "stu_phone='" + student.getStu_phone() + "',";
        sql = sql + " where ID=" + student.getId();
        return this.dBservice.updateSql(sql);
    }

    @Override
    public int delete(Student student) {
        String sql = "delete from students where stu_id=" + student.getStu_ID();
        return this.dBservice.updateSql(sql);
    }

    @Override
    public List<Student> getlist() {
        List<Student> list = new ArrayList<Student>();
        List<Map<String, Object>> list_temp = this.dBservice.selectSql("select * from students");
        for(Map<String, Object>map : list_temp){
            Student student = new Student();
            student.setId(Integer.valueOf(map.get("ID").toString()));
            student.setStu_ID(map.get("stu_id").toString());
            student.setStu_name(map.get("stu_name").toString());
            student.setStu_clazz(map.get("stu_clazz").toString());
            student.setStu_phone(map.get("stu_phone").toString());
            list.add(student);
        }
        return list;
    }
}
